﻿<!--
    Project: Web Dev Assignment 4
    File: game.aspx
    Author: Ryan Pink
    Date Modified: November 19, 2013
    Description: 
        This source page loads in the content frame of the game engine. This page loads session data on start-up
        each time, and processes the last round of gameplay before preparing the next.
-->

<%@ Page Language="C#" Debug="true" CodeBehind="~/game.aspx.cs" %>

<!-- Ahh, the meat of the whole assignment. This script section contains all of the delicious
     code that runs the functions of the game. They are commented below. -->
<script runat="server" >

    public string playerName;   // self-explanatory
    public int playerGuess;     // the last guess from the payer

    public int gameRound = 1;   // what round of the game are we on?
    
    public int highNumber;      // what's the upper boundary for guessing?
    public int lowNumber = 1;   // and the lower bound?
    public int randNumber;      // and the special secret number.


    /*
     * LoadSessionData()
     * Parameters:
     *      object sender: the object that called the method
     *      EventArgs e  : system arguments associated with the call
     * Description  :
     *      This function interprets the current session state and grabs/initializes information
     *      in preparation for game processing code.
     */
    protected void LoadSessionData( Object sender, EventArgs e ) {

        // Do we have a session?
        if (Session["player_name"] != null)
        {
            // we do! pull the data.
            playerName = Session["player_name"].ToString();
            highNumber = int.Parse(Session["high_number"].ToString());
            lowNumber = int.Parse(Session["low_number"].ToString());
            randNumber = int.Parse(Session["rand_number"].ToString());

            // is this the first round?
            if (Session["game_round"] != null)
            {
                // nope, grab the round number.
                gameRound = int.Parse(Session["game_round"].ToString());
            }

            lblGuessStatus.Text = "Alright, " + playerName + ". Are you ready?";
            
            // has the player guessed yet?
            if ( Request.Form["player_guess"] != null)
            {
                // they have! grab it.
                playerGuess = int.Parse(Request.Form["player_guess"]);
                Session["player_guess"] = playerGuess;
                
                // call the processing method.
                ProcessGuess();
            }
        }
        else
        {
            // we don't have a session yet. let's populate one.
            playerName = Request.Form["player_name"];
            highNumber = int.Parse(Request.Form["high_number"]);

            randNumber = new Random().Next(1, highNumber);
            
            Session["player_name"] = playerName;
            Session["high_number"] = highNumber;
            Session["low_number"] = lowNumber;
            Session["rand_number"] = randNumber;
            Session["game_round"] = gameRound;
        }
    }


    /*
     * ProcessGuess()
     * Parameters:
     *      N/A
     * Description  :
     *      This function interprets the values stored in session data and processes it to prepare
     *      for the next round of play.
     */
    protected void ProcessGuess()
    {
        // Increment the game round.
        gameRound++;
        Session["game_round"] = gameRound;
        
        // check the player's guess
        if (playerGuess < lowNumber || playerGuess > highNumber)    // out of bounds?
        {
            lblGuessStatus.Text = "That is not within the range I gave you. Do you need a break?";
        }
        else if (playerGuess < randNumber)                          // too low?
        {
            lblGuessStatus.Text = "<b>Too low!</b> Come on, " + playerName + "! Pull up your socks...";
            lowNumber = playerGuess;
            Session["low_number"] = lowNumber;
        }
        else if (playerGuess > randNumber)                          // too high?
        {
            lblGuessStatus.Text = "<b>Too high!</b> Step down off that cloud, " + playerName + "...";
            highNumber = playerGuess;
            Session["high_number"] = highNumber;
        }
        else if (playerGuess == randNumber)                         // is it CORRECT?!
        {
            lblGuessStatus.Text = "Congratulations! You guess it correctly!";
            Response.Redirect("winnerisyou.aspx");
        }
        else                                                        // have I been fooled? how did we get here?!
        {
            lblGuessStatus.Text = "Hmm, you did something sneaky. Top drawer, ol' bean!";
            Response.Redirect("name.aspx");
        }
    }
</script>


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
    <link href="style.css" rel="stylesheet" />
</head>

<body>
    <!-- This placeholder tag loads the session information when the page loads. -->
    <asp:PlaceHolder runat="server" ID="loadSessionData" OnLoad="LoadSessionData" />

    <table>
    <tr><td style="width: 60%;">

    <!-- This is the main form for the Hi-Lo game. This page uses it to accept a number from the player. -->
    <form id="frmHiLo" runat="server" method="post" action="game.aspx" defaultfocus="player_guess">
        <div class="content">

            <!-- Tell them what round it is. -->
            <h3>Let's Play! .:: Round <% =gameRound %> ::.</h3><br />
            <asp:Label ID="lblGuessStatus" runat="server" Text="" /><br />

            <!-- Tell the player the valid range. -->
            Please guess a number between <% =lowNumber %> and <% =highNumber %>.
            <asp:TextBox id="player_guess" runat="server" />
            
            <!------------ High Number Validators ------------>

            <!-- Validate the number is entered (i.e. not empty) -->
            <asp:RequiredFieldValidator id="playerGuessRequired" 
                                        runat="server" 
                                        ControlToValidate="player_guess" 
                                        ErrorMessage="<br />A number is required." 
                                        Display="Dynamic"
                                        ForeColor="Red" />

            <!-- Validate the number is in fact a number -->
            <asp:CompareValidator id="playerGuessCompare" 
                                  runat="server" 
                                  ControlToValidate="player_guess" 
                                  Type="Integer"
                                  Operator="DataTypeCheck"
                                  ErrorMessage="<br />Please enter an integer number." 
                                  Display="Dynamic"
                                  ForeColor="Red" />
            <br />

            <asp:Button ID="btnSubmit" Runat=server Text="Guess'd!" PostBackUrl="game.aspx" />
        </div>
    </form>

    <!-- This section is used to provide information to the user. -->
    </td><td class="guide">

        <div class="header" style="position: relative; top: -3px;">
            <!-- Using the player's name establishes trust, I've been told. -->
            <h2>Good Luck, <% =playerName %>! ::.</h2>
        </div>
        <div class="content">
            <p><b>Do you need a reminder of the rules?</b></p>
            <p><b>Here's how to play:</b></p>
            <blockquote>
                <i>The player gives a large number, and the game picks a random number between 1 and that number. 
                Then the player attempts to guess the random number, each time being told whether they are
                too high or too low.<br /><br />

                When the player guesses correctly, <b>they win!</b></i>
            </blockquote>
            <p><b>Are you lost? Want to get out of here? Click the banner at the top to return to the main page!</b></p>
        </div>

    </td></tr>
    </table>
</body>

</html>
